using System;
using System.Collections.Generic;
using System.Data;

namespace Widgetsphere.Core.DataAccess
{
	/// <summary>
	/// Object that runs free form SQL against the database
	/// </summary>
	public partial class SQLFreeText
	{
		#region Class Members

		private readonly string _sql;
		private readonly List<IDbDataParameter> _parameters = new List<IDbDataParameter>();
		private DateTime _lastAccessed;

		#endregion

		#region Constructors

		internal SQLFreeText(string connectionString, string sql)
		{			
			_sql = sql;
			_lastAccessed = DateTime.Now;
		}

		#endregion

		#region Properties

		public List<IDbDataParameter> Parameters
		{
			get { return _parameters; }
		}

		#endregion

		#region Methods

		internal IDbCommand GetCommand()
		{
			var returnCommand = DatabaseHelper.GetCommand();
			returnCommand.CommandText = _sql;
			returnCommand.Parameters.Clear();
			returnCommand.CommandType = CommandType.Text;
			foreach (var param in _parameters)
			{
				returnCommand.Parameters.Add(ParameterHelper.GetInstance().GetParameterClone(param));
			}
			_lastAccessed = DateTime.Now;
			return returnCommand;
		}
	
		#endregion

	}
}
